﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PowerBooking.RSE.Utility.Helpers;
using PowerBooking.RSE.Contract.Enums;

namespace PowerBooking.RSE.Domain.Models
{
    /// <summary>
    /// 蜘蛛监视器领域对象
    /// </summary>
    public class SpiderMonitor
    {
        /// <summary>
        /// 蜘蛛ID
        /// </summary>
        public long SpiderID { get; private set; }

        /// <summary>
        /// 城市数
        /// </summary>
        public long Cities { get; private set; }

        /// <summary>
        /// 筛选项数
        /// </summary>
        public long Filters { get; private set; }

        /// <summary>
        /// 餐厅数
        /// </summary>
        public long Restaurants { get; private set; }

        /// <summary>
        /// 异常
        /// </summary>
        public Exception Exception { get; private set; }

        /// <summary>
        /// 蜘蛛状态
        /// </summary>
        public SpiderStatusEnum SpiderStatus { get; private set; }

        internal SpiderMonitor InitDomain(long spiderID, long cities, long filters, long restaurants, SpiderStatusEnum spiderStatusEnum)
        {
            SpiderID = spiderID;
            Cities = cities;
            Filters = filters;
            Restaurants = restaurants;
            SpiderStatus = spiderStatusEnum;
            return this;
        }

        internal void UpdateStatus(SpiderStatusEnum spiderStatusEnum)
        {
            SpiderStatus = spiderStatusEnum;
            var spiderMonitorDictionary = CacheHelper.Get<Dictionary<long, SpiderMonitor>>("SpiderMonitorDictionary");
            spiderMonitorDictionary[SpiderID].SpiderStatus = SpiderStatus;
            CacheHelper.Set("SpiderMonitorDictionary", spiderMonitorDictionary, DateTime.Now.AddYears(1));
        }

        internal void UpdateError(Exception exception)
        {
            Exception = exception;
            var spiderMonitorDictionary = CacheHelper.Get<Dictionary<long, SpiderMonitor>>("SpiderMonitorDictionary");
            spiderMonitorDictionary[SpiderID].Exception = Exception;
            spiderMonitorDictionary[SpiderID].SpiderStatus = SpiderStatusEnum.Stop;
            CacheHelper.Set("SpiderMonitorDictionary", spiderMonitorDictionary, DateTime.Now.AddYears(1));
        }

        internal void Update(long cities, long filters, long restaurants)
        {
            Cities += cities;
            Filters += filters;
            Restaurants += restaurants;

            var spiderMonitorDictionary = CacheHelper.Get<Dictionary<long, SpiderMonitor>>("SpiderMonitorDictionary");
            spiderMonitorDictionary[SpiderID].Cities = Cities;
            spiderMonitorDictionary[SpiderID].Filters = Filters;
            spiderMonitorDictionary[SpiderID].Restaurants = Restaurants;
            CacheHelper.Set("SpiderMonitorDictionary", spiderMonitorDictionary, DateTime.Now.AddYears(1));
        }
    }
}
